package com.djzhou.gmms.service.role.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Service;

import com.djzhou.gmms.model.authority.TbAuthority;
import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.menu.TbMenu;
import com.djzhou.gmms.model.menu.TbMenuAuthority;
import com.djzhou.gmms.model.role.MenuAuthorityVo;
import com.djzhou.gmms.model.role.RoleMenuAuthorityVo;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.role.TbRoleAuthority;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.role.RoleAuthorityService;
import com.djzhou.gmms.util.session.UserSession;
import com.djzhou.util.DateUtil;

public class RoleAuthorityServiceImpl  extends BaseServiceImpl implements RoleAuthorityService{

	@Override
	public void add(TbRoleAuthority t) {
		
		
	}

	@Override
	public void delete(Integer id) {
		
		
	}

	@Override
	public TbRoleAuthority queryById(Integer id) {
		
		return null;
	}

	@Override
	public List<TbRoleAuthority> queryList(HashMap<String, Object> hashMap) {
		return null;
	}

	@Override
	public PageBean<TbRoleAuthority> queryPageBean(
			HashMap<String, Object> hashMap, int pageSize, int currentPage) {
		
		return null;
	}

	@Override
	public void update(TbRoleAuthority t) {
		
		
	}

	@Override
	public List<TbRoleAuthority> queryList(Integer roleId) {
		String hql="from TbRoleAuthority ra where ra.role.roleId=? order by ra.menu.backupData";
		return roleAuthorityDAO.query(hql, roleId);
	}

	@Override
	public void save(String menuAuthorityString, Integer menuAuthorityRoleId,TbUser user) {
		String deleteHql="delete from TbRoleAuthority ra where ra.role.roleId=?";
		roleAuthorityDAO.delete(deleteHql, menuAuthorityRoleId);
		String[] array=menuAuthorityString.split(",");
		for(int i=0;i<array.length;i+=2){
			TbRoleAuthority ra=new TbRoleAuthority();
			TbMenu menu=menuDAO.queryByID(TbMenu.class, Integer.parseInt(array[i]));
			ra.setMenu(menu);
			TbAuthority authority=authorityDAO.queryByID(TbAuthority.class, Integer.parseInt(array[i+1]));
			ra.setAuthority(authority);
			TbRole role=roleDAO.queryByID(TbRole.class, menuAuthorityRoleId);
			ra.setRole(role);
			ra.setCreateTime(DateUtil.getNowTime());
			ra.setLastModifyTime(DateUtil.getNowTime());
			ra.setOpenFlag(1);
			ra.setRemark("");
			ra.setUser(user);
			roleAuthorityDAO.insert(ra);
		}
		
	}

	@Override
	public String queryAuthorityCodeByMenuAndRole(String roleIds, Integer menuId) {
		String authorityCode=",";
		String hql="from TbRoleAuthority ra where ra.role.roleId in("+roleIds+") and ra.menu.menuId=?";
		List<TbRoleAuthority> list=roleAuthorityDAO.query(hql,menuId);
		for(TbRoleAuthority obj:list){
				authorityCode+=obj.getAuthority().getAuthorityNo()+",";
		}
		return authorityCode;
	}
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleMenuAuthorityVo> queryRoleMenuAuthorityVoList(Integer roleId) {
		String hqlStr="from TbMenu menu where menu.deleteFlag=0 and menu.openFlag=1 and menu.publicMenu=0";
		if(UserSession.getSessionModel().getUser().getUserId()!=1)hqlStr+=" and menu.backupData>100";
		List<TbMenu> menuList=menuDAO.query(hqlStr);
		List<RoleMenuAuthorityVo> list=new ArrayList<RoleMenuAuthorityVo>();
		for(TbMenu menu:menuList){
			RoleMenuAuthorityVo vo=new RoleMenuAuthorityVo();
			vo.setMenu(menu);
			//通过菜单查询菜单下的权限
			List<MenuAuthorityVo> menuAuthorityVoList=new ArrayList<MenuAuthorityVo>();
			List<TbMenuAuthority> menuAuthorityList=menuAuthorityDAO.query("from TbMenuAuthority ma where ma.menu.menuId=?",menu.getMenuId());
			for(TbMenuAuthority ma:menuAuthorityList){
				MenuAuthorityVo menuAuthorityVo=new MenuAuthorityVo();
				menuAuthorityVo.setAuthority(ma.getAuthority());
				if(roleId!=null&&roleId!=0){
					String hql="from TbRoleAuthority ra where ra.menu.menuId=? and ra.role.roleId=?";
					List<TbRoleAuthority> roleAuthorityList=roleAuthorityDAO.query(hql,ma.getMenu().getMenuId(),roleId);
					for(TbRoleAuthority ra:roleAuthorityList){
						if(ra.getAuthority().getAuthorityId()==ma.getAuthority().getAuthorityId()){
							menuAuthorityVo.setOwnFlag(1);
						}
					}
				}else{
					menuAuthorityVo.setOwnFlag(0);
				}
				menuAuthorityVoList.add(menuAuthorityVo);
			}
			vo.setMenuAuthorityVoList(menuAuthorityVoList);
			list.add(vo);
		}
		return list;
	}

}
